package com.qf.realm;


import com.qf.service.UserService;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;


@Configuration
public class ShrioConfig {
    /*1.常见realm*/
    @Bean
    public Realm realm(){
        return new CustomRealm();
    }

    /*2.创建安全管理器*/
    @Bean
    public DefaultWebSecurityManager securityManager(Realm realm){
        /*1.创建安全管理器*/
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //2.注入realm
        securityManager.setRealm(realm);
        //3.返回安全管理器
        return securityManager;
    }

    /*2.创建ShiroFilterChainDefinition*/
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(UserService userService){
        //1.创建过滤器链对象
        DefaultShiroFilterChainDefinition shiroFilterChainDefinition = new DefaultShiroFilterChainDefinition();
        //2.调用userService查询全部过滤器链的信息
        LinkedHashMap<String,String>  filterChainDefinition = new LinkedHashMap<String,String>();
        filterChainDefinition.put("/user/logout","logout");
        filterChainDefinition.put("/css/**","anon");
        filterChainDefinition.put("/layer/**","anon");
        filterChainDefinition.put("/images/**","anon");
        filterChainDefinition.put("/js/**","anon");
        filterChainDefinition.put("/leave/**","anon");
        filterChainDefinition.put("/user/**","anon");
        filterChainDefinition.put("/**","authc");
        //3.设置过滤器链信息
        shiroFilterChainDefinition.addPathDefinitions(filterChainDefinition);
        //4.返回过滤器链对象
        return shiroFilterChainDefinition;
    }

}
